package com.unlcn.ils.erpInterfaces.service.buss.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.unlcn.ils.erpInterfaces.dao.mapper.SmcityMapper;
import com.unlcn.ils.erpInterfaces.dao.mapper.SmcustMapper;
import com.unlcn.ils.erpInterfaces.dao.mapper.SmcustomermapMapper;
import com.unlcn.ils.erpInterfaces.dao.model.Smcity;
import com.unlcn.ils.erpInterfaces.dao.model.Smcustomer;
import com.unlcn.ils.erpInterfaces.dao.mapper.SmcustomerMapper;
import com.unlcn.ils.erpInterfaces.dao.model.Smcustomermap;
import com.unlcn.ils.erpInterfaces.service.buss.ISmcustomerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * SMCUSTOMER 客户信息  服务实现类
 * </p>
 *
 * @author liangpeng
 * @since 2018-06-08
 */
@Service
public class SmcustomerServiceImpl extends ServiceImpl<SmcustomerMapper, Smcustomer> implements ISmcustomerService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SmcustomerServiceImpl.class);

    @Autowired
    private SmcustomermapMapper smcustomermapMapper;

    @Autowired
    private SmcustomerMapper smcustomerMapper;

    @Autowired
    private SmcustMapper smcustMapper;

    @Autowired
    private SmcityMapper smcityMapper;

    @Override
    public Long getCustomerId(String customername, Long vccityid, Long custId) {

        // 查询在erp是否存在
        Wrapper<Smcustomer> smcustomerWrapper = new EntityWrapper<>();
        smcustomerWrapper.eq("VCCUSTOMERNAME", customername);
        List<Smcustomer> smcustomerList = smcustomerMapper.selectList(smcustomerWrapper);

        if (CollectionUtils.isEmpty(smcustomerList)) {

            // 如果erp也没有找到对应客户，则在 erp 插入对应客户信息（并且插入客户对应关系表）

            //获取城市信息
            Smcity smcity = smcityMapper.selectById(vccityid);

            Long vccustomerid = null;
            if (smcity == null) {
                LOGGER.error(customername+"：客户未找到对应城市");
            } else {
                // 在 erp 插入客户信息
                Smcustomer smcustomer = new Smcustomer();
                // 主键
                vccustomerid = smcustomerMapper.getNextIlineid();
                smcustomer.setIlineid(vccustomerid);
                // 客户编号
                smcustomer.setVccustomerno(smcustomerMapper.getMaxCustomerNo());
                // 客户名称
                smcustomer.setVccustomername(customername);
                smcustomer.setVccompanyname(customername);
                smcustomer.setVccontact("来源于wms导入");
                //smcustomer.setVctel("010-5123221");
                smcustomer.setVcprovince(smcity.getVcprovince());
                smcustomer.setVccityname(smcity.getVccityname());
                smcustomer.setVcuserno("system");
                smcustomer.setIcustid(custId);
                smcustomer.setDtdate(new Date());
                smcustomerMapper.insert(smcustomer);
            }

            return vccustomerid;
        } else {
            return smcustomerList.get(0).getIlineid();
        }
    }
}
